<?php
namespace app\admin\controller;
use catetree\Catetree;
use think\Controller;
class Category extends controller
{
    public function lst()
    {
        $category=new catetree();
        $categoryObj=db('category');
        if(request()->isPost()){
            $data=input('post.');
            $category->categorySort($data['sort'],$categoryObj);
            $this->success('排序成功',url('lst'));

        }
        $categoryRes=db('category')->order('sort DESC')->select();
        $categoryRes=$category->catetree($categoryRes);
        $this->assign('categoryRes',$categoryRes);
        return view();
    }
    public function add()
    {
        $category=new catetree();
        $categoryObj=db('category');
        if(request()->isPost()){
            $data=input('post.');
            //添加验证数据
            $validate = validate('category');
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            $add=$categoryObj->insert($data);
            if($add){
                $this->success('添加分类成功',url('category/lst'));
            }else{
                $this->error('添加分类失败，请返回修改',url('category/add'));
            }
            return;

        }
        $categoryRes=db('category')->order('sort DESC')->select();
        $categoryRes=$category->catetree($categoryRes);

        $this->assign('categoryRes',$categoryRes);
        return view();
    }
    public function edit()
    {

        $categoryObj=db('category');
        if (request()->isPost()){
            $data=input('post.');

        $save=db('category')->update($data);
        if($save !== false){
            $this->success('修改分类成功','lst');
        }else{
            $this->error('修改分类失败');
        }
        return;
        }
        $category=new catetree();
        $categoryRes=$categoryObj->order('sort DESC')->select();
        $categoryRes=$category->catetree($categoryRes);
        $categorys=db('category')->find(input('id'));
        $this->assign([
            'categoryRes'=>$categoryRes,
            'categorys'=>$categorys,
        ]);
        return view();
    }

    public function del($id)
    {
        $category=db('category');
        $catetree=new catetree();
        $sonids=$catetree->childrenids($id,$category);
        $sonids[]=intval($id);
        $arrsys=[1,2,3];
        $arrres=array_intersect($arrsys,$sonids);
        if($arrres){
            $this->error('系统内置文章分类，不许被删除');
        }
        //删除分类前判断该分类下的文章和文章相关缩略图
        $article=db('article');
        foreach($sonids as $k=>$v){
            $artRes=$article->field('id,thumb')->where(array('category_id'=>$v))->select();
                foreach ($artRes as $k1=>$v1){
                    $thumbSrc=IMG_UPLOADS.$v1['thumb'];
                    if(file_exists($thumbSrc)){
                        @unlink($thumbSrc);
                    }
                    $article->delete($v1['id']);
                }
        }
        $del=$category->delete($sonids);
        if($del){
            $this->success('删除数据成功','lst');
        }else{
            $this->error('删除数据失败');
        }
        return ;
    }
    //单独上传图片源码
    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('brand_logo');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS .'static'. DS .'uploads');
            if($info){
                return  $info->getSaveName();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();  die;
            }
        }
    }
}
